In [1]:
%matplotlib inline

import matplotlib
import numpy as np
import matplotlib.pyplot as plt

In [2]:
#import setuptools
%load_ext Cython

difference between cdef and cpdef


In [3]:
%%cython
cdef f1(int x):
    return x*x

cpdef f2(int x):
    return x*x

cpdef f3(int x):
    return f1(x)

In [4]:
#dir()

In [5]:
f2(3)


Out[5]:
9

f1 is not visible since defined via "cdef"


In [7]:
f1(3)


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-7-a636accabfae> in <module>()
----> 1 f1(3)

NameError: name 'f1' is not defined

In [8]:
f3(3)


Out[8]:
9

filling an numpy array


In [9]:
%%cython
cpdef fibseq(float[:] x):
    cdef int n
    cdef int i
    n = len(x)
    
    x[0] = 1.
    x[1] = 1.
    for i in range(2,n):
        x[i] = x[i-1]+x[i-2]

In [11]:
x = np.zeros((43,), np.float32)

In [12]:
fibseq(x)

In [13]:
x


Out[13]:
array([  1.00000000e+00,   1.00000000e+00,   2.00000000e+00,
         3.00000000e+00,   5.00000000e+00,   8.00000000e+00,
         1.30000000e+01,   2.10000000e+01,   3.40000000e+01,
         5.50000000e+01,   8.90000000e+01,   1.44000000e+02,
         2.33000000e+02,   3.77000000e+02,   6.10000000e+02,
         9.87000000e+02,   1.59700000e+03,   2.58400000e+03,
         4.18100000e+03,   6.76500000e+03,   1.09460000e+04,
         1.77110000e+04,   2.86570000e+04,   4.63680000e+04,
         7.50250000e+04,   1.21393000e+05,   1.96418000e+05,
         3.17811000e+05,   5.14229000e+05,   8.32040000e+05,
         1.34626900e+06,   2.17830900e+06,   3.52457800e+06,
         5.70288700e+06,   9.22746500e+06,   1.49303520e+07,
         2.41578160e+07,   3.90881680e+07,   6.32459840e+07,
         1.02334152e+08,   1.65580128e+08,   2.67914272e+08,
         4.33494400e+08], dtype=float32)

In [14]:
ratio = x[0:-1]/x[1:]

In [16]:
plt.plot(ratio,'.');



In [17]:
(np.sqrt(5)-1)/2.


Out[17]:
0.6180339887498949

In [18]:
plt.semilogy(ratio-(np.sqrt(5)-1)/2,'.');



In [19]:
ratio


Out[19]:
array([ 1.        ,  0.5       ,  0.66666669,  0.60000002,  0.625     ,
        0.61538464,  0.61904764,  0.61764705,  0.61818182,  0.6179775 ,
        0.61805558,  0.61802578,  0.61803716,  0.61803281,  0.61803442,
        0.61803383,  0.61803406,  0.61803395,  0.61803401,  0.61803401,
        0.61803401,  0.61803401,  0.61803401,  0.61803401,  0.61803401,
        0.61803401,  0.61803401,  0.61803401,  0.61803401,  0.61803401,
        0.61803401,  0.61803401,  0.61803401,  0.61803401,  0.61803401,
        0.61803401,  0.61803401,  0.61803401,  0.61803401,  0.61803401,
        0.61803401,  0.61803401], dtype=float32)

In [20]:
import seaborn as sbs

In [21]:
sbs.plt.plot(ratio)


Out[21]:
[<matplotlib.lines.Line2D at 0x7f9ccec88210>]

basic : fibonacchi example (pure python and cython)


In [22]:
def pyfib(n):
    a,b = 1,1
    for i in range(n):
        a,b = a+b, a
    return a

In [23]:
pyfib(5)


Out[23]:
13

In [24]:
%%cython
def fib(int n):
    cdef int i,a,b
    a,b = 1,1
    for i in range(n):
        a,b = a+b, a
    return a

In [25]:
fib(10)


Out[25]:
144

In [26]:
[fib(i) for i in range(10)]


Out[26]:
[1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

In [27]:
pyfib(10)


Out[27]:
144

In [28]:
%timeit fib(10)
%timeit pyfib(10)


The slowest run took 28.11 times longer than the fastest. This could mean that an intermediate result is being cached.
10000000 loops, best of 3: 67.9 ns per loop
The slowest run took 4.74 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 1.06 µs per loop

distance function (pure python and cython)


In [29]:
%%cython
cpdef float distance(float[:] x, float[:] y):
    cdef int i
    cdef int n = x.shape[0]
    cdef float d = 0.0
    for i in range(n):
        d += (x[i]-y[i])**2
    return d

In [30]:
import numpy as np
x = np.array([1,2,3], np.float32)
y = np.array([7,6,5], np.float32)
dist = distance(x,y)
print dist


56.0

In [31]:
def pydistance(x, y, n):
    d = 0.0
    for i in range(n):
        d += (x[i]-y[i])**2
    return d

In [32]:
%timeit distance(x,y)
%timeit pydistance(x,y,3)


The slowest run took 15.03 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 1.33 µs per loop
100000 loops, best of 3: 16.8 µs per loop

In [ ]: